package com.gshrms.web.controller.system;

import com.gshrms.common.annotation.Log;
import com.gshrms.common.constant.CacheConstants;
import com.gshrms.common.core.controller.BaseController;
import com.gshrms.common.core.domain.entity.SysUser;
import com.gshrms.common.core.domain.model.RegisterBody;
import com.gshrms.common.enums.BusinessType;
import com.gshrms.common.utils.SecurityUtils;
import com.gshrms.common.utils.StringUtils;
import com.gshrms.common.utils.file.FileUploadUtils;
import com.gshrms.framework.config.ServerConfig;
import com.gshrms.framework.web.service.SysRegisterService;
import com.gshrms.recruitment.entity.param.SysUserParam;
import com.gshrms.system.mapper.SysUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.gshrms.common.constant.Constants;
import com.gshrms.common.core.domain.AjaxResult;
import com.gshrms.common.core.domain.model.LoginBody;
import com.gshrms.framework.web.service.SysLoginService;

import java.io.IOException;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
@Api(tags = "个人信息")
@RequestMapping("/user/info")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ServerConfig serverConfig;


    @GetMapping("/get")
    @ApiOperation("获取用户个人信息、简历详情")
    public AjaxResult<SysUser> getInfo() {

        Long userId = getUserId();
        SysUser sysUser = sysUserMapper.selectById(userId);
        sysUser.setUserId(null);
        sysUser.setPassword(null);
        return AjaxResult.success(sysUser);
    }


    @ApiOperation("修改个人信息")
    @PostMapping("/update")
    @Log(title = "修改应聘者个人信息", businessType = BusinessType.UPDATE)
    public AjaxResult login(@Validated @RequestBody SysUserParam userParam) {

        Long userId = getUserId();
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(userParam, sysUser);
        sysUser.setUserId(userId);
        sysUser.setIsUpdate(1);

        //签字上传图片
        if (StringUtils.isNotBlank(userParam.getSignImage()) && !userParam.getSignImage().startsWith("http")) {
            FileUploadUtils fileUploadUtils = new FileUploadUtils();
            try {
                String url = serverConfig.getUrl() + Constants.RESOURCE_PREFIX + "/" + fileUploadUtils.saveBase64Image(userParam.getSignImage());
                sysUser.setSignImage(url);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        sysUserMapper.updateById(sysUser);
        return success();
    }

}
